/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package managedBeans;

import facade.SuiLibFacade;
import facade.exception.NotExistingObjectException;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.component.html.HtmlDataTable;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import valueObject.RoleVO;
import valueObject.UniversityVO;
import valueObject.UserVO;

/**
 *
 * @author David
 */
@ManagedBean
public class universitiesMB {

    
    private HtmlDataTable table;
    private UserVO userVO;
    private boolean success = false;
    private boolean successEdit = false;
    private String password;
    private String universityUser;
    private String name;
    private String address;
    private String phoneNumber;
    private String type;
    private String web;
    private String joinDate; //Date
    private String description;
    private String location;
    private String image; //byte[]
    
    @EJB
    SuiLibFacade suilibFacade;
    
    List<UniversityVO> universities;
    
    /**
     * Creates a new instance of universitiesMB
     */
    public universitiesMB() {
        userVO = new UserVO();  
    }

    /**
     * @return the universityUser
     */
    public String getUniversityUser() {
        return universityUser;
    }

    /**
     * @param universityUser the universityUser to set
     */
    public void setUniversityUser(String universityUser) {
        this.universityUser = universityUser;
    }

    /**
     * @return the name
     */
    public String getName() {
        return name;
    }

    /**
     * @param name the name to set
     */
    public void setName(String name) {
        this.name = name;
    }

    /**
     * @return the address
     */
    public String getAddress() {
        return address;
    }

    /**
     * @param address the address to set
     */
    public void setAddress(String address) {
        this.address = address;
    }

    /**
     * @return the phoneNumber
     */
    public String getPhoneNumber() {
        return phoneNumber;
    }

    /**
     * @param phoneNumber the phoneNumber to set
     */
    public void setPhoneNumber(String phoneNumber) {
        this.phoneNumber = phoneNumber;
    }

    /**
     * @return the type
     */
    public String getType() {
        return type;
    }

    /**
     * @param type the type to set
     */
    public void setType(String type) {
        this.type = type;
    }

    /**
     * @return the web
     */
    public String getWeb() {
        return web;
    }

    /**
     * @param web the web to set
     */
    public void setWeb(String web) {
        this.web = web;
    }

    /**
     * @return the joinDate
     */
    public String getJoinDate() {
        return joinDate;
    }

    /**
     * @param joinDate the joinDate to set
     */
    public void setJoinDate(String joinDate) {
        this.joinDate = joinDate;
    }

    /**
     * @return the description
     */
    public String getDescription() {
        return description;
    }

    /**
     * @param description the description to set
     */
    public void setDescription(String description) {
        this.description = description;
    }

    /**
     * @return the location
     */
    public String getLocation() {
        return location;
    }

    /**
     * @param location the location to set
     */
    public void setLocation(String location) {
        this.location = location;
    }

    /**
     * @return the image
     */
    public String getImage() {
        return image;
    }

    /**
     * @param image the image to set
     */
    public void setImage(String image) {
        this.image = image;
    }
    
    public List<UniversityVO> getUniversities() {
        universities = suilibFacade.getAllUniversityVO();
        return universities;
    }
    
    public void createUniversity() {
        UserVO UserVO = new UserVO();
        UserVO.setUsername(userVO.getUsername());
        UserVO.setPassword(userVO.getPassword());
        RoleVO roleVO = new RoleVO();
        roleVO.setId(2);
        UserVO.setRole(roleVO);
        System.out.println(UserVO.getUsername() + " " + UserVO.getPassword());
        suilibFacade.createUser(UserVO);
        UniversityVO universityVo = new UniversityVO();
        universityVo.setName(name);
        universityVo.setUserName(userVO.getUsername());
        suilibFacade.createUniversity(universityVo);
        System.out.println(name + " " + universityUser);
    }

    
    public void editUniversity() {
        try {
            UserVO userVo = (UserVO) FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("user");
            UniversityVO universityVO = suilibFacade.findUniversity(userVo.getUsername());
            universityVO.setName(name);
            universityVO.setLocation(location);
            universityVO.setAddress(address);
            universityVO.setPhoneNumber(phoneNumber);
            universityVO.setType(type);
            universityVO.setWeb(web);
            universityVO.setDescription(description);
            suilibFacade.updateUniversity(universityVO);
            successEdit = true;
        } catch (NotExistingObjectException ex) {
            Logger.getLogger(universitiesMB.class.getName()).log(Level.SEVERE, null, ex);
            successEdit =false;
        }
      
    }
    /**
     * @return the password
     */
    public String getPassword() {
        return password;
    }

    /**
     * @param password the password to set
     */
    public void setPassword(String password) {
        this.password = password;
    }

    /**
     * @return the userVO
     */
    public UserVO getUserVO() {
        return userVO;
    }

    /**
     * @param userVO the userVO to set
     */
    public void setUserVO(UserVO userVO) {
        this.userVO = userVO;
    }
    
    
    public void deleteFormList() {
        UniversityVO vo =(UniversityVO) table.getRowData();
        System.out.println("Vamos a eliminar ---------------->" +vo.getUserName());
    }

    /**
     * @return the table
     */
    public HtmlDataTable getTable() {
        return table;
    }

    /**
     * @param table the table to set
     */
    public void setTable(HtmlDataTable table) {
        this.table = table;
    }

    /**
     * @return the success
     */
    public boolean isSuccess() {
        return success;
    }

    /**
     * @return the successEdit
     */
    public boolean isSuccessEdit() {
        return successEdit;
    }
}
